package com.ht.medicaltreatment.activity.haocai

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.HaoCaiListAdapter
import com.ht.medicaltreatment.bean.Page
import com.ht.medicaltreatment.viewmodel.HaoCaiListViewModel
import kotlinx.android.synthetic.main.activity_haocai_list_layout.btnPositive
import kotlinx.android.synthetic.main.activity_haocai_list_layout.etCode
import kotlinx.android.synthetic.main.activity_haocai_list_layout.imgRight
import kotlinx.android.synthetic.main.activity_haocai_list_layout.recyclerView

//耗材列表
class HaoCaiListActivity:BaseActivity(), OnItemChildClickListener {
    private val model by viewModels<HaoCaiListViewModel>()

    private val page = 1
    private var numberName = ""
    private val adapter = HaoCaiListAdapter()

    override fun isViewStup() = true

    override fun getLayoutId() = R.layout.activity_haocai_list_layout

    override fun init() {

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemChildClickListener(this)

        initData()

        //新建
        imgRight.setOnClickListener {
            startActivity(CreateZhuanTiActivity::class.java)
        }

        //查询
        btnPositive.setOnClickListener {
            numberName = etCode.text.toString()
            initData()
        }
    }

    override fun onRestart() {
        super.onRestart()
        initData()
    }

    private fun initData() {
        showProgress()
        model.getList("$page",numberName){
            dismissProgress()
            val authSign = it.data.authSign
            if(authSign == 1){
                imgRight.visibility = View.VISIBLE
            }
            adapter.setAuthSign(authSign)
            adapter.setNewInstance(it.data.pageBean.pageList)
        }
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val page = adapter.getItem(position) as Page
        when(view.id){
            R.id.tvName,
            R.id.tvStatus -> {
                val intent = Intent(this,UpdateZhuanTiActivity::class.java)
                intent.putExtra("projectId",page.id)
                startActivity(intent)
            }

            //申请，审批
            R.id.tvChange,
            R.id.tvDelete ->{
                val intent = Intent(this,HaoCaiShenQingListActivity::class.java)
                intent.putExtra("projectId",page.id)
                startActivity(intent)
            }
        }
    }
}
